package com.android.djlmapp.activity.home.web.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.djlmapp.R;
import com.android.djlmapp.base.BaseActivity;
import com.android.djlmapp.listener.read.GetPhotoUrlListener;
import com.android.djlmapp.tools.read.WriteCardUtils;
import com.android.readcard.activity.WriteCardActivity;
import com.android.readcard.bean.ReadIccidAndWriteCardInfo;
import com.android.readcard.eventbus.Event;
import com.android.readcard.eventbus.EventBusUtil;
import com.android.readcard.eventbus.EventCodes;
import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @Author wangziheng
 * @ProjectName djlm
 * @ClassName ResetCardActivity
 * @package com.android.djlmapp.activity.home.web.base
 * @data 2019/1/11 16:55
 * @Describe 废卡重置
 * @Change
 */
public class ResetCardActivity extends BaseActivity {
    @BindView(R.id.tv_title_kaika)
    TextView mTitleTv;
    @BindView(R.id.tv_title_content)
    TextView mContentTv;
    @BindView(R.id.cl_dkyw)
    ConstraintLayout mItemLayout;
    @BindView(R.id.iv_icon_kaika)
    ImageView mImg;
    private Unbinder mUnBinder;
    @BindView(R.id.iv_top_kf)
    ImageView mCustomIcon;
    @BindView(R.id.iv_top_msg)
    ConstraintLayout mConstraintLayout;
    @BindView(R.id.kk_line)
    View kkLine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_card_layout);
        mUnBinder = ButterKnife.bind(this);
        mTitleTv.setText("废卡重置");
        mContentTv.setText("重置写入失败的卡");
        kkLine.setVisibility(View.GONE);
        Glide.with(this).load(R.mipmap.home_fkcz).into(mImg);
        mItemLayout.setOnClickListener(v -> {
            // 蓝牙对话框
            startActivity(new Intent(ResetCardActivity.this, WriteCardActivity.class).putExtra("WriteType", 0));
        });
        mConstraintLayout.setVisibility(View.GONE);
        mCustomIcon.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }


    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected void receiveEvent(Event event) {
        if (event.getCode() == EventCodes.EventReadCardCode.B) {
            // 返回iccid
            ReadIccidAndWriteCardInfo iccidData = (ReadIccidAndWriteCardInfo) event.getData();
            String iccidNum = iccidData.getIccidNum();
            int writeType = iccidData.getWriteType();
            String imsi = iccidData.getImsi();
            // imsi
            if (writeType == 0) {
                WriteCardUtils.clearCard(iccidNum, imsi, new GetPhotoUrlListener() {
                    @Override
                    public void backPhotoUrl(int backType, String result) {
                        // eventbus通知WriteCardActivity,废卡重置完成了,成功失败都要提示并且关闭写卡界面
                        Event<String> clearCardEvent = new Event<>(EventCodes.EventReadCardCode.E);
                        EventBusUtil.sendEvent(clearCardEvent);
                        WriteCardUtils.showMsg(ResetCardActivity.this, "废卡重置结果", result);
                    }
                });
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnBinder.unbind();
    }
}
